package com.janko.springbootbase.controller;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @ClassName zipController
 * @Description TODO:
 * @Author janko
 * @Date 2020/11/20 17:40
 */
public class ZipController {

    public static void main(String[] args) {
        zipFileBuffer();
    }
    public static void zipFileBuffer() {
        String FILE_NAME = "aa.jpg";
        String JPG_FILE = "D:\\image\\uy125电子说明书.pdf";
        File zipFile = new File("D:\\image\\图片.zip");
        try (ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
             BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(zipOut)) {
            long beginTime = System.currentTimeMillis();
            for (int i = 0; i < 10; i++) {
                try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(JPG_FILE))) {
                    zipOut.putNextEntry(new ZipEntry(FILE_NAME + i));
                    int temp = 0;
                    while ((temp = bufferedInputStream.read()) != -1) {
                        bufferedOutputStream.write(temp);
                    }
                }
            }
            printInfo(beginTime);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printInfo(long beginTime) {
        System.out.println("执行时间：" + (System.currentTimeMillis() - beginTime)  + "毫秒");
    }


    

}
